---
title: "搭建本地Ubuntu/Debian软件包仓库"
categories:
- linux
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org44cbc11">Intro</a></li>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#step-1-configure-server">Step 1: Configure Server</a></li>
<li><a href="#configure-clients">Configure clients</a></li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org44cbc11">
<h2 id="org44cbc11">Intro</h2>
<div class="outline-text-2" id="text-org44cbc11">
<blockquote>
<p>
<a href="https://www.tecmint.com/setup-local-repositories-in-ubuntu/">https://www.tecmint.com/setup-local-repositories-in-ubuntu/</a>
<a href="https://www.howtoforge.com/local%5C_debian%5C_ubuntu%5C_mirror%5C_p2">https://www.howtoforge.com/local\_debian\_ubuntu\_mirror\_p2</a> -–—
</p>
</blockquote>
<p>
When today traffic and casual internet speeds is measured in teens of
Giga over an eye blink even for ordinary Internet clients, what's the
purpose of setting a local repository cache on LAN's you may ask?
</p>
<p>
One of the reasons is to reduce Internet bandwidth and high speed on
pulling packages from local cache. But, also, another major reason
should be privacy. Let's imagine that clients from your organization are
Internet restricted, but their Linux boxes need to regular system
updates on software and security or just need new software packages. To
go further picture, a server that runs on a private network, contains
and serves secret sensitive information only for a restricted network
segment, and should never be exposed to public Internet.
</p>
<p>
This are just a few reasons why you should build a local repository
mirror on your LAN, delegate an edge server for this job and configure
internal clients to pull out software form its cache mirror.
</p>
<p>
Ubuntu provides apt-mirror package to synchronize local cache with
official Ubuntu repositories, mirror that can be configured through a
HTTP or FTP server to share its software packages with local system
clients.
</p>
<p>
For a complete mirror cache your server needs at least 120G free space
reserved for local repositories.
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org1d12729">
<h2 id="requirements"><a id="org1d12729"></a>Requirements</h2>
<div class="outline-text-2" id="text-requirements">
<ul class="org-ul">
<li>Min 120G free space</li>
<li>Proftpd server installed and configured in anonymous mode</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orge71ca55">
<h2 id="step-1-configure-server"><a id="orge71ca55"></a>Step 1: Configure Server</h2>
<div class="outline-text-2" id="text-step-1-configure-server">
<ol class="org-ol">
<li>The first thing you may want to do is to identify the closest and
fastest Ubuntu mirrors near you're location by visiting Ubuntu
Archive Mirror page and select your country.</li>
</ol>
<div class="figure">
<p><img alt="Setup-Local-Repository-01-620x293.png" src="Setup-Local-Repository-01-620x293.png"/>
</p>
<p><span class="figure-number">Figure 1: </span>Ubuntu Archive Mirror</p>
</div>
<p>
If your country provides more mirrors you should identify mirror address
and do some tests based on ping or traceroute results.
</p>
<div class="figure">
<p><img alt="Setup-Local-Repository-02-620x336.png" src="Setup-Local-Repository-02-620x336.png"/>
</p>
<p><span class="figure-number">Figure 2: </span>Select Mirror Location</p>
</div>
<ol class="org-ol">
<li>The next step is to install required software for setting up local
mirror repository. Install apt-mirror and proftpd packages and
configure proftpd as standalone system daemon.</li>
</ol>
<pre class="example">
sudo apt-get install apt-mirror proftpd-basic
</pre>
<p>
proftp换成自己熟悉的ftp服务器，或者使用http服务器。
</p>
<ol class="org-ol">
<li>Now it's time to configure apt-mirror server. Open and edit
/etc/apt/mirror.list file by adding your nearest locations (Step 1)
– optional, if default mirrors are fast enough or you're not in a
hurry – and choose your system path where packages should be
downloaded. By default apt-mirror uses /var/spool/apt-mirror location
for local cache but on this tutorial we are going to use change
system path and point set base\_path directive to /opt/apt-mirror
location.</li>
</ol>
<pre class="example">
############# config ##################
#
set base_path    /data/apt-mirror # 特地挂载了一个大硬盘
#
# set mirror_path  $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  &lt;running host architecture&gt;
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############

deb http://archive.ubuntu.com/ubuntu artful main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu artful-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu artful-proposed main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu artful-backports main restricted universe multiverse

# 去掉了src，不然硬盘不够用
#deb-src http://archive.ubuntu.com/ubuntu artful main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-security main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu artful-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu
</pre>
<p>
下载完之后，占了磁盘79G！
</p>
<p>
Also you can uncomment or add other source list before clean directive
– including Debian sources – depending on what Ubuntu versions your
clients use. You can add sources from 12.04, if you like but be aware
that adding more sources requires more free space.
</p>
<p>
For Debian source lists visit
<a href="https://wiki.debian.org/SourcesList">Debian Wiki</a> or
<a href="http://debgen.simplylinux.ch/">Debian Sources</a> List Generator.
</p>
<ol class="org-ol">
<li>All you need to do now is, just create path directory and run
apt-mirror command to synchronize official Ubuntu repositories with
our local mirror.</li>
</ol>
<pre class="example">
sudo apt-mirror
</pre>
<p>
As you can see apt-mirror proceeds with indexing and downloading
archives presenting total number of downloaded packages and their size.
As we can imagine 110-120 GB is large enough to take some time to
download.
</p>
<p>
Once the initial download is completed, future downloads will be small.
</p>
<p>
5~7.
配置FTP服务器，不一定需要使用proftpd，使用自己熟悉的ftp服务器，或者http服务器都行。
</p>
<ol class="org-ol">
<li>For a daily apt-mirror synchronization you can also create a system
schedule job to run at 2 AM every day. Run crontab command, select
your preferred editor then add the following line syntax.</li>
</ol>
<pre class="example">
$ sudo crontab –e
# append line:
0  2  *  *  *  /usr/bin/apt-mirror &gt;&gt; /opt/apt-mirror/mirror/archive.ubuntu.com/ubuntu/apt-mirror.log
</pre>
<p>
Now every day at 2 AM your system repository cache will synchronize with
Ubuntu official mirrors and create a log file.
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org6ce4dfc">
<h2 id="configure-clients"><a id="org6ce4dfc"></a>Configure clients</h2>
<div class="outline-text-2" id="text-configure-clients">
<ol class="org-ol">
<li>To configure local Ubuntu clients, edit /etc/apt/source.list on
client computers to point to the IP address or hostname of apt-mirror
server – replace http protocol with ftp, then update system.</li>
</ol>
<pre class="example">
deb ftp://192.168.1.13/ubuntu trusty universe
deb ftp://192.168.1.13/ubuntu trusty main restricted
deb ftp://192.168.1.13/ubuntu trusty-updates main restricted
## Ad so on….
</pre>
<ol class="org-ol">
<li>To view repositories you can actually open a browser and point to
your server IP address of domain name using FTP protocol.</li>
</ol>
<div class="figure">
<p><img alt="Setup-Local-Repository-16-620x304.jpg" src="Setup-Local-Repository-16-620x304.jpg"/>
</p>
<p><span class="figure-number">Figure 3: </span>View Local Repositories</p>
</div>
<p>
The same system applies also to Debian clients and servers, the only
change needed are debian mirror and sources list.
</p>
<p>
Also if you install a fresh Ubuntu or Debian system, provide your local
mirror manually whit ftp protocol when installer asks which repository
to use.
</p>
<p>
The great thing about having your own local mirror repositories is that
you're always on current and your local clients don't have to connect to
Internet to install updates or software.
</p>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
